package cn.edu.scujcc;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlbumService {
	@Autowired
	private AlbumRepository repo;
		
	public List<Album> getAllAlbum(){
		List<Album>result = null;
		result = repo.findAll();
		return result;
	}
	public Album getAlbum(String Album) {
		Album result = null;
		Optional<Album> op = repo.findById(Album);
		if (op.isPresent()) {
			result = op.get();
			}
		return result;
		}
	public Album createAlbum(Album a) {
		Album result = null;
		result = repo.save(a);
		return result;
	}
	public Album updateAlbum(Album a) {
		Album result = null;
		result = repo.findById(a.getId()).get();
		if(result !=null) {
		if(a.getName()!=null){
			result.setName(a.getName());
		}
		if(a.getCover()!=null){
			result.setCover(a.getCover());
		}
		if(a.getMusic()!=null){
			result.setMusic(a.getMusic());
		}
		result = repo.save(result);
		}
		return result;
	}
}
